package com.itkaien.javaeebased;

import java.io.*;
import org.junit.Test;

public class fileDemo {

    @Test

    public void test() throws IOException {

        File file = new File("a");

        File file1=new File("b");

        copyDir(file, file1);



    }

    private void copyDir(File src, File target) throws IOException {
        target.mkdirs();
        if (src.isDirectory()) {
            File[] files = src.listFiles();
            for (File file : files) {
                if (file.isFile()) {
                    FileInputStream fileInputStream = new FileInputStream(file);
                    FileOutputStream fileOutputStream = new FileOutputStream(new File(target, file.getName()));
                    byte[] buffer=new byte[1024];
                    int lean;
                    while ((lean=fileInputStream.read(buffer))!=-1){
                        fileOutputStream.write(buffer,0,lean);
                    }
                    fileInputStream.close();
                    fileOutputStream.close();
                }else {
                    //递归调用
                    copyDir(file,new File(target,file.getName()));
                }
            }


        }
    }


    @Test

    public void test1() throws IOException{

        BufferedInputStream a = new BufferedInputStream(new FileInputStream("a.md"));


        BufferedOutputStream b = new BufferedOutputStream(new FileOutputStream("b.txt"));



        byte[] buffer=new byte[1024];

        int lean;

        while ((lean=a.read(buffer))!=-1){

            b.write(buffer,0,lean);
        }

        a.close();
        b.close();


    }

    @Test

    public void test2() throws IOException{

        FileReader fileReader = new FileReader("a.md");

        FileWriter fileWriter = new FileWriter("b.txt");

        BufferedReader bufferedReader = new BufferedReader(fileReader);

        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);


        int lean;

        while ((lean=bufferedReader.read())!=-1){
            bufferedWriter.write(lean);
        }
    }

    @Test
    public void test3() throws IOException{

        FileReader fileReader = new FileReader("a.md");

        FileWriter fileWriter = new FileWriter("b.txt",true);

        BufferedReader bufferedReader = new BufferedReader(fileReader);

        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        String line;
        while ((line=bufferedReader.readLine())!=null) {
            bufferedWriter.write(line);
            bufferedWriter.newLine();
        }
       bufferedWriter.close();
        bufferedReader.close();
    }
}
